package net.jgrm.jetcache.config;

import com.alicp.jetcache.CacheManager;
import com.alicp.jetcache.anno.config.EnableCreateCacheAnnotation;
import com.alicp.jetcache.anno.config.EnableMethodCache;
import lombok.extern.slf4j.Slf4j;
import net.jgrm.jetcache.monitor.JetCacheMonitorService;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * JetCache 自动配置类
 * 基于JetCache 2.7.3版本，简化配置
 *
 * @author jgrm
 */
@Slf4j
@Configuration
@ConditionalOnClass({CacheManager.class})
@ConditionalOnProperty(prefix = "jetcache", name = "enabled", havingValue = "true", matchIfMissing = true)
@AutoConfigureAfter({RedisAutoConfiguration.class})
@EnableConfigurationProperties(JetCacheProperties.class)
@EnableMethodCache(basePackages = "net.jgrm")
@EnableCreateCacheAnnotation
public class JetCacheAutoConfiguration {

    /**
     * JetCache监控服务
     */
    @Bean
    @ConditionalOnProperty(prefix = "jetcache.monitor", name = "enabled", havingValue = "true", matchIfMissing = true)
    public JetCacheMonitorService jetCacheMonitorService(JetCacheProperties properties) {
        log.info("Initializing JetCache monitor service");
        return new JetCacheMonitorService(properties);
    }
}